import { http } from "@/utils/http";

export interface AdminItem {
  id: number;
  name: string;
  username?: string | null;
  rname?: string | null;
  phone?: string | null;
  email?: string | null;
  imageUrl?: string | null;
  createTime?: string;
}

export interface AdminListParams {
  page?: number;
  limit?: number;
  name?: string;
}

export interface AdminListResult {
  code: number;
  data: {
    total: number;
    list: AdminItem[];
    pageNum: number;
    pageSize: number;
    size: number;
    startRow: number;
    endRow: number;
    pages: number;
    prePage: number;
    nextPage: number;
    isFirstPage: boolean;
    isLastPage: boolean;
    hasPreviousPage: boolean;
    hasNextPage: boolean;
    navigatePages: number;
    navigatepageNums: number[];
    navigateFirstPage: number;
    navigateLastPage: number;
  };
  message: string;
}

/** 管理员列表 */
export const getAdminList = (data: AdminListParams) => {
  return http.request<AdminListResult>("post", "admin/list", { data });
};

export interface AdminPayload {
  name: string;
  username?: string;
  phone?: string | null;
  email?: string | null;
  imageUrl?: string | null;
}

export interface AdminCreateResult {
  code: number;
  data: number;
  message: string;
}

export interface AdminUpdateResult {
  code: number;
  data: boolean;
  message: string;
}

export interface AdminDeleteResult {
  code: number;
  data: boolean;
  message: string;
}

/** 新增管理员 */
export const createAdmin = (data: AdminPayload) => {
  return http.request<AdminCreateResult>("post", "admin/add", { data });
};

/** 更新管理员 */
export const updateAdmin = (id: number, data: Partial<AdminPayload>) => {
  return http.request<AdminUpdateResult>("post", "admin/update", {
    data: { id, ...data }
  });
};

/** 删除管理员 */
export const deleteAdmin = (id: number) => {
  return http.request<AdminDeleteResult>("get", "admin/delete", {
    params: { id }
  });
};

// ========== 管理员角色相关 ==========
export interface UpdateAdminRoleParams {
  adminId: number;
  roleIds: number[];
}

export interface UpdateAdminRoleResult {
  code: number;
  data: boolean;
  message: string;
}

/** 更新管理员角色 */
export const updateAdminRole = (data: UpdateAdminRoleParams) => {
  return http.request<UpdateAdminRoleResult>("post", "admin/updateAdminRole", {
    data
  });
};

/** 根据id查询管理员 */
export const getAdminById = (id: number) => {
  return http.request<AdminItem>("get", "admin/info", {
    params: { id }
  });
};
